package input;

import instance.Instance;
import instance.InstanceEj2;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;


import problem.ej2.Link;



public class Problem2Input implements Input{
	
	
	private String inputFile;
	private List<Instance> instances;
	
	public Problem2Input(String inputFile) {
		this.inputFile = inputFile;

	}
    //nos guardamos todas las instancias del archivo de entrada para procesarlos de a uno
	
	@Override
	public void readFile() throws Exception {
				
		BufferedReader bufferedReader = new BufferedReader(new FileReader(
				new File(inputFile)));

		instances = new ArrayList<Instance>();

		while (bufferedReader.ready()) {
			Instance instance =  readInstance(bufferedReader);
			instances.add( instance);
		}
			
		bufferedReader.close();		
	}

	@Override
	public List<Instance> getInstances() {
	
		return instances;
	}

	@Override
	public Instance readInstance(BufferedReader bufferedReader)
			throws Exception {
		int l1 = 0;
		int l2 = 0;
		int cost = 0;

		List <Link> instance = new ArrayList<Link>();
		InstanceEj2 subInstance ;
		String line = bufferedReader.readLine();
		
		StringTokenizer tokens = new StringTokenizer(line, ";");
		
		while(tokens.hasMoreTokens()){
			String tup = tokens.nextToken();
			StringTokenizer tupla =  new StringTokenizer(tup);

		l1 =Integer.parseInt(tupla.nextToken().trim());
		l2= Integer.parseInt(tupla.nextToken().trim());
		cost = Integer.parseInt(tupla.nextToken().trim());
		Link terna = new Link(l1,l2,cost);
		instance.add(terna); 
		}
		subInstance = new InstanceEj2(instance);

				
		return subInstance;
		
		
		
		
		
	}
		

}
